package com.lxmes.device.service.impl;

import java.util.List;

import com.lxmes.common.config.RuoYiConfig;
import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.*;
import com.lxmes.device.utils.WaterMarkUtils;
import com.lxmes.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.device.mapper.NcEmGzbxMapper;
import com.lxmes.device.domain.NcEmGzbx;
import com.lxmes.device.service.INcEmGzbxService;

/**
 * 设备故障报修Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-11
 */
@Service
public class NcEmGzbxServiceImpl implements INcEmGzbxService 
{
    @Autowired
    private NcEmGzbxMapper ncEmGzbxMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询设备故障报修
     * 
     * @param id 设备故障报修主键
     * @return 设备故障报修
     */
    @Override
    public NcEmGzbx selectNcEmGzbxById(Long id)
    {
        return ncEmGzbxMapper.selectNcEmGzbxById(id);
    }

    /**
     * 查询设备故障报修列表
     * 
     * @param ncEmGzbx 设备故障报修
     * @return 设备故障报修
     */
    @Override
    public List<NcEmGzbx> selectNcEmGzbxList(NcEmGzbx ncEmGzbx)
    {
        return ncEmGzbxMapper.selectNcEmGzbxList(ncEmGzbx);
    }

    /**
     * 新增设备故障报修
     * 
     * @param ncEmGzbx 设备故障报修
     * @return 结果
     */
    @Override
    public int insertNcEmGzbx(NcEmGzbx ncEmGzbx) throws Exception {
        ncEmGzbx.setCljd("0");
        String number= NewNumber.createNewNumber(ncEmGzbxMapper.getNewCode(DateUtils.getDate()));
        ncEmGzbx.setBxdh("GZBX-"+number);
        ncEmGzbx.setBxdh("GZBX-"+DateUtils.dateTimeNow());
        SysUser user = sysUserService.selectUserById(SecurityUtils.getUserId());
        ncEmGzbx.setBxr(user.getNickName());
        ncEmGzbx.setLxfs(user.getPhonenumber());
        ncEmGzbx.setFssj(DateUtils.getNowDate());
        ncEmGzbx.setUserId(SecurityUtils.getUserId().toString());
        ncEmGzbx.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isNotNull(ncEmGzbx.getGztp())){
            String[] path = ncEmGzbx.getGztp().split(",");
            for (int i = 0; i <path.length ; i++) {
                WaterMarkUtils.resizeImage(path[i],360,600);
                WaterMarkUtils.creat(path[i],"报修人："+ncEmGzbx.getBxr(),"发生时间："+ncEmGzbx.getFssj());
                WaterMarkUtils.commPressPicCycle(path[i],0.8);
            }
        }
        if(ncEmGzbx.getDeptId()==null|| ncEmGzbx.getDeptId().longValue() == 0L){
            ncEmGzbx.setDeptId(SecurityUtils.getDeptId());
        }

        this.validEntityBeforeSaveOrUpdate(ncEmGzbx);
        ncEmGzbxMapper.insertNcEmGzbx(ncEmGzbx);
        if(RuoYiConfig.isIsSendSms()){
            user=sysUserService.selectUserById(102L);
            if(StringUtils.isNotNull(user)){
                if(StringUtils.isNotBlank(user.getPhonenumber())){
                    Sample.sendSms(user.getPhonenumber(),ncEmGzbx.getBxdh(),"SMS_241057261");
                }
            }
        }
        return 1;
    }

    public void  validEntityBeforeSaveOrUpdate(NcEmGzbx ncEmGzbx){
        int v=ncEmGzbxMapper.validEntityBeforeSaveOrUpdate(ncEmGzbx);
        if(v>0){
            throw new ServiceException("故障报修单ID"+ncEmGzbx.getBxdh()+"已存在");
        }

    }

    /**
     * 修改设备故障报修
     * 
     * @param ncEmGzbx 设备故障报修
     * @return 结果
     */
    @Override
    public int updateNcEmGzbx(NcEmGzbx ncEmGzbx)
    {
        this.validEntityBeforeSaveOrUpdate(ncEmGzbx);
        if(ncEmGzbx.getDeptId()==null|| ncEmGzbx.getDeptId().longValue() == 0){
            ncEmGzbx.setDeptId(SecurityUtils.getDeptId());
        }
        ncEmGzbx.setUpdateTime(DateUtils.getNowDate());
        ncEmGzbx.setUserId(SecurityUtils.getUserId().toString());
        return ncEmGzbxMapper.updateNcEmGzbx(ncEmGzbx);
    }

    /**
     * 批量删除设备故障报修
     * 
     * @param ids 需要删除的设备故障报修主键
     * @return 结果
     */
    @Override
    public int deleteNcEmGzbxByIds(Long[] ids)
    {
        return ncEmGzbxMapper.deleteNcEmGzbxByIds(ids);
    }

    /**
     * 删除设备故障报修信息
     * 
     * @param id 设备故障报修主键
     * @return 结果
     */
    @Override
    public int deleteNcEmGzbxById(Long id)
    {
        return ncEmGzbxMapper.deleteNcEmGzbxById(id);
    }

    @Override
    public int apiCancelFailureReport(String reportId) {
        NcEmGzbx report = ncEmGzbxMapper.selectByReportId(reportId);
        if(!report.getCljd().equals("0")){
            throw new ServiceException("此报修单已接单不能取消");
        }
        return ncEmGzbxMapper.deleteNcEmGzbxById(report.getId());
    }

    @Override
    public NcEmGzbx selectNcEmGzbxByReportId(String reportId) {
        return ncEmGzbxMapper.selectByReportId(reportId);
    }
}
